local love = require "love"

local BULLET_VELOCITY = 500
local BULLET_RADIUS = 2

local Bullet = {
    radius = BULLET_RADIUS
}


function Bullet.new(self, object)
    object = object or {}
    self.__index = self
    setmetatable(object, self)

    return object
end

function Bullet.collide(self, asteroid)
    local distance = math.sqrt((self.x - asteroid.x) ^ 2 + (self.y - asteroid.y) ^ 2)
    if distance <= self.radius + asteroid.radius then
        self.survive = false
        return true
    else
        self.survive = true
        return false
    end
end

function Bullet.move(self)
    -- boundary limit
    if self.x + self.radius <= 0 or self.x + self.radius >= love.graphics.getWidth() or
        self.y + self.radius <= 0 or self.y + self.radius >= love.graphics.getHeight() then
            self.survive = false
    end

    if self.survive then
        local dt = love.timer.getDelta()
        self.x = self.x + math.cos(self.angle) * BULLET_VELOCITY * dt
        self.y = self.y - math.sin(self.angle) * BULLET_VELOCITY * dt
    end
end

function Bullet.draw(self)
    love.graphics.setColor(1, 1, 1)
    if self.survive then
        love.graphics.circle("fill", self.x, self.y, self.radius)
    end
end

return Bullet
